package com.dhcc.bpm.common.utils;

import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.servers.Server;
import org.openapitools.codegen.CodegenOperation;
import org.openapitools.codegen.SupportingFile;
import org.openapitools.codegen.languages.JavaClientCodegen;
import org.openapitools.codegen.languages.features.CXFServerFeatures;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class JavaClientGenerateUtil extends JavaClientCodegen {
	private List<String> userTemplates = new ArrayList<String>();
	/**
	 * 初始化配置
	 */
	public JavaClientGenerateUtil(String serviceId) {
		super();

		String serviceName = ServiceUtils.serviceId2Name(serviceId, false);
		apiPackage = "com.dhcc.client.api." + serviceName; // "org.openapitools.api"
		modelPackage = "com.dhcc.client.model." + serviceName; // "org.openapitools.model"
		invokerPackage = "com.dhcc.client.api"; // "org.openapitools.api"
		groupId = "com.dhcc.client";
		setDeveloperName("");
		additionalProperties().put(CXFServerFeatures.LOAD_TEST_DATA_FROM_FILE, "true");
		setTemplateDir("Java");		//自定义模版路径，默认是在javaSpring目录
		setEnablePostProcessFile(true);  //NOTE: To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI).
	}

	/**
	 * 处理选项 可以自定义
	 */
	@Override
	public void processOpts() {
		super.processOpts();
		additionalProperties.put("isTest", "hello world!");

		supportingFiles.add(new SupportingFile("GatewayClientApi.mustache",  (sourceFolder + ".com.dhcc.client.api").replace(".", File.separator), "GatewayClientApi.java"));
		supportingFiles.add(new SupportingFile("PublicApi.mustache",  (sourceFolder + ".com.dhcc.client.api").replace(".", File.separator), "PublicApi.java"));

		supportingFiles.add(new SupportingFile("entity" + File.separator + "DecryptE.mustache",  (sourceFolder + ".com.dhcc.entity").replace(".", File.separator), "DecryptE.java"));
		supportingFiles.add(new SupportingFile("entity" + File.separator + "EncryptE.mustache",  (sourceFolder + ".com.dhcc.entity").replace(".", File.separator), "EncryptE.java"));
		supportingFiles.add(new SupportingFile("entity" + File.separator + "SecretE.mustache",  (sourceFolder + ".com.dhcc.entity").replace(".", File.separator), "SecretE.java"));
		supportingFiles.add(new SupportingFile("entity" + File.separator + "GatewayRequest.mustache",  (sourceFolder + ".com.dhcc.entity").replace(".", File.separator), "GatewayRequest.java"));
		supportingFiles.add(new SupportingFile("entity" + File.separator + "GatewayResponse.mustache",  (sourceFolder + ".com.dhcc.entity").replace(".", File.separator), "GatewayResponse.java"));
		supportingFiles.add(new SupportingFile("entity" + File.separator + "AccessToken.mustache",  (sourceFolder + ".com.dhcc.entity").replace(".", File.separator), "AccessToken.java"));

		supportingFiles.add(new SupportingFile("excetion" + File.separator + "BussesExcetion.mustache",  (sourceFolder + ".com.dhcc.excetion").replace(".", File.separator), "BussesExcetion.java"));
		supportingFiles.add(new SupportingFile("util" + File.separator + "ConstantUtil.mustache",  (sourceFolder + ".com.dhcc.util").replace(".", File.separator), "ConstantUtil.java"));
		supportingFiles.add(new SupportingFile("util" + File.separator + "MapSignUtil.mustache",  (sourceFolder + ".com.dhcc.util").replace(".", File.separator), "MapSignUtil.java"));
		supportingFiles.add(new SupportingFile("util" + File.separator + "NewSignUtil.mustache",  (sourceFolder + ".com.dhcc.util").replace(".", File.separator), "NewSignUtil.java"));
		supportingFiles.add(new SupportingFile("util" + File.separator + "RequestBodyUtil.mustache",  (sourceFolder + ".com.dhcc.util").replace(".", File.separator), "RequestBodyUtil.java"));
		supportingFiles.add(new SupportingFile("util" + File.separator + "Sm4Util.mustache",  (sourceFolder + ".com.dhcc.util").replace(".", File.separator), "Sm4Util.java"));
		supportingFiles.add(new SupportingFile("util" + File.separator + "SMUtils.mustache",  (sourceFolder + ".com.dhcc.util").replace(".", File.separator), "SMUtils.java"));
		supportingFiles.add(new SupportingFile("util" + File.separator + "StringUtil.mustache",  (sourceFolder + ".com.dhcc.util").replace(".", File.separator), "StringUtil.java"));
		supportingFiles.add(new SupportingFile("util" + File.separator + "PropertiesUtil.mustache",  (sourceFolder + ".com.dhcc.util").replace(".", File.separator), "PropertiesUtil.java"));
		supportingFiles.add(new SupportingFile("util" + File.separator + "AssertUtil.mustache",  (sourceFolder + ".com.dhcc.util").replace(".", File.separator), "AssertUtil.java"));

		writeOptional(outputFolder, new SupportingFile("gateway.mustache", "src.main.resources".replace(".", File.separator), "gateway.properties"));
		writeOptional(outputFolder, new SupportingFile("PublicApiTest.mustache", "src.test.java.com.dhcc.client.api".replace(".", File.separator), "PublicApiTest.java"));
	}

	@Override
	public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, List<Server> servers) {
		CodegenOperation op = super.fromOperation(path, httpMethod, operation, servers);
		Boolean isGet  = false;
		Boolean isPost = false;
		Boolean isOther = false;
		if ("GET".equals(op.httpMethod)) {
			op.operationId = op.operationId.replace("UsingGET", "");
			isOther = true;
			isGet = true;
		}
		if ("POST".equals(op.httpMethod)) {
			op.operationId = op.operationId.replace("UsingPOST", "");
			isPost = true;
			if (op.getHasCookieParams() || op.getHasQueryParams() || op.getHasFormParams() || op.getHasHeaderParams() || op.getHasCookieParams()) {
				isOther = true;
			}
		}

		op.vendorExtensions.put("isGet", isGet);
		op.vendorExtensions.put("isPost", isPost);
		op.vendorExtensions.put("isOther", isOther);
		return op;
	}
}